home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / xdr / xdr_floa.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  8KB  |  276 lines

  1. /* @(#)xdr_float.c    2.1 88/07/29 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * xdr_float.c, Generic XDR routines impelmentation.
  36.  *
  37.  * Copyright (C) 1984, Sun Microsystems, Inc.
  38.  *
  39.  * These are the "floating point" xdr routines used to (de)serialize
  40.  * most common data items.  See xdr.h for more info on the interface to
  41.  * xdr.
  42.  */
  43.  
  44. #include <stdio.h>
  45.  
  46. #include "..\xdr\types.h"
  47. #include "..\xdr\xdr.h"
  48.  
  49. /*
  50.  * NB: Not portable.
  51.  * This routine works on Suns (Sky / 68000's) and Vaxen.
  52.  */
  53.  
  54. #if defined(vax) || defined(WIN32)
  55.  
  56. /* What IEEE single precision floating point looks like on a Vax */
  57. struct    ieee_single {
  58.     unsigned int    mantissa: 23;
  59.     unsigned int    exp     : 8;
  60.     unsigned int    sign    : 1;
  61. };
  62.  
  63. /* Vax single precision floating point */
  64. struct    vax_single {
  65.     unsigned int    mantissa1 : 7;
  66.     unsigned int    exp       : 8;
  67.     unsigned int    sign      : 1;
  68.     unsigned int    mantissa2 : 16;
  69. };
  70.  
  71. #define VAX_SNG_BIAS    0x81
  72. #define IEEE_SNG_BIAS    0x7f
  73.  
  74. static struct sgl_limits {
  75.     struct vax_single s;
  76.     struct ieee_single ieee;
  77. } sgl_limits[2] = {
  78.     {{ 0x7f, 0xff, 0x0, 0xffff },    /* Max Vax */
  79.     { 0x0, 0xff, 0x0 }},        /* Max IEEE */
  80.     {{ 0x0, 0x0, 0x0, 0x0 },    /* Min Vax */
  81.     { 0x0, 0x0, 0x0 }}        /* Min IEEE */
  82. };
  83. #endif /* vax */
  84.  
  85. bool_t
  86. xdr_float(xdrs, fp)
  87.     register XDR *xdrs;
  88.     register float *fp;
  89. {
  90.  
  91. #if !defined(mc68000) && !defined(sparc) && !defined(mips) && !defined(mmax) && !defined(_X86_)
  92.     struct ieee_single is;
  93.     struct vax_single vs, *vsp;
  94.     struct sgl_limits *lim;
  95.     int i;
  96. #endif
  97.     switch (xdrs->x_op) {
  98.  
  99.     case XDR_ENCODE:
  100. #if defined(WIN32) || defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
  101.         return (XDR_PUTLONG(xdrs, (long *)fp));
  102. #else
  103.         vs = *((struct vax_single *)fp);
  104.         for (i = 0, lim = sgl_limits;
  105.             i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  106.             i++, lim++) {
  107.             if ((vs.mantissa2 == lim->s.mantissa2) &&
  108.                 (vs.exp == lim->s.exp) &&
  109.                 (vs.mantissa1 == lim->s.mantissa1)) {
  110.                 is = lim->ieee;
  111.                 goto shipit;
  112.             }
  113.         }
  114.         is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS;
  115.         is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2;
  116.     shipit:
  117.         is.sign = vs.sign;
  118.         return (XDR_PUTLONG(xdrs, (long *)&is));
  119. #endif
  120.  
  121.     case XDR_DECODE:
  122. #if defined(WIN32) || defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
  123.         return (XDR_GETLONG(xdrs, (long *)fp));
  124. #else
  125.         vsp = (struct vax_single *)fp;
  126.         if (!XDR_GETLONG(xdrs, (long *)&is))
  127.             return (FALSE);
  128.         for (i = 0, lim = sgl_limits;
  129.             i < sizeof(sgl_limits)/sizeof(struct sgl_limits);
  130.             i++, lim++) {
  131.             if ((is.exp == lim->ieee.exp) &&
  132.                 (is.mantissa == lim->ieee.mantissa)) {
  133.                 *vsp = lim->s;
  134.                 goto doneit;
  135.             }
  136.         }
  137.         vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS;
  138.         vsp->mantissa2 = is.mantissa;
  139.         vsp->mantissa1 = (is.mantissa >> 16);
  140.     doneit:
  141.         vsp->sign = is.sign;
  142.         return (TRUE);
  143. #endif
  144.  
  145.     case XDR_FREE:
  146.         return (TRUE);
  147.     }
  148.     return (FALSE);
  149. }
  150.  
  151. /*
  152.  * This routine works on Suns (Sky / 68000's) and Vaxen.
  153.  */
  154.  
  155. #ifdef vax
  156. /* What IEEE double precision floating point looks like on a Vax */
  157. struct    ieee_double {
  158.     unsigned int    mantissa1 : 20;
  159.     unsigned int    exp       : 11;
  160.     unsigned int    sign      : 1;
  161.     unsigned int    mantissa2 : 32;
  162. };
  163.  
  164. /* Vax double precision floating point */
  165. struct  vax_double {
  166.     unsigned int    mantissa1 : 7;
  167.     unsigned int    exp       : 8;
  168.     unsigned int    sign      : 1;
  169.     unsigned int    mantissa2 : 16;
  170.     unsigned int    mantissa3 : 16;
  171.     unsigned int    mantissa4 : 16;
  172. };
  173.  
  174. #define VAX_DBL_BIAS    0x81
  175. #define IEEE_DBL_BIAS    0x3ff
  176. #define MASK(nbits)    ((1 << nbits) - 1)
  177.  
  178. static struct dbl_limits {
  179.     struct    vax_double d;
  180.     struct    ieee_double ieee;
  181. } dbl_limits[2] = {
  182.     {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff },    /* Max Vax */
  183.     { 0x0, 0x7ff, 0x0, 0x0 }},            /* Max IEEE */
  184.     {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},        /* Min Vax */
  185.     { 0x0, 0x0, 0x0, 0x0 }}                /* Min IEEE */
  186. };
  187.  
  188. #endif /* vax */
  189.  
  190.  
  191. bool_t
  192. xdr_double(xdrs, dp)
  193.     register XDR *xdrs;
  194.     double *dp;
  195. {
  196.     register long *lp;
  197. #if !defined(WIN32) && !defined(mc68000) && !defined(sparc) && !defined(mips) && !defined(mmax) && !defined(_X86_)
  198.     struct    ieee_double id;
  199.     struct    vax_double vd;
  200.     register struct dbl_limits *lim;
  201.     int i;
  202. #endif
  203.  
  204.     switch (xdrs->x_op) {
  205.  
  206.     case XDR_ENCODE:
  207. #if defined(WIN32) || defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
  208.         lp = (long *)dp;
  209. #else
  210.         vd = *((struct vax_double *)dp);
  211.         for (i = 0, lim = dbl_limits;
  212.             i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  213.             i++, lim++) {
  214.             if ((vd.mantissa4 == lim->d.mantissa4) &&
  215.                 (vd.mantissa3 == lim->d.mantissa3) &&
  216.                 (vd.mantissa2 == lim->d.mantissa2) &&
  217.                 (vd.mantissa1 == lim->d.mantissa1) &&
  218.                 (vd.exp == lim->d.exp)) {
  219.                 id = lim->ieee;
  220.                 goto shipit;
  221.             }
  222.         }
  223.         id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS;
  224.         id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3);
  225.         id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) |
  226.                 (vd.mantissa3 << 13) |
  227.                 ((vd.mantissa4 >> 3) & MASK(13));
  228.     shipit:
  229.         id.sign = vd.sign;
  230.         lp = (long *)&id;
  231. #endif
  232. #if defined(WIN32) || defined(_X86_) 
  233.         return (XDR_PUTLONG(xdrs, lp+1) && XDR_PUTLONG(xdrs, lp));
  234. #else
  235.         return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp));
  236. #endif
  237.     case XDR_DECODE:
  238. #if defined(WIN32) || defined(mc68000) || defined(sparc) || defined(mips) || defined(mmax) || defined(_X86_)
  239.         lp = (long *)dp;
  240. #if defined(WIN32) || defined(_X86_)
  241.         return (XDR_GETLONG(xdrs, lp+1) && XDR_GETLONG(xdrs, lp));
  242. #else
  243.         return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp));
  244. #endif
  245. #else
  246.         lp = (long *)&id;
  247.         if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp))
  248.             return (FALSE);
  249.         for (i = 0, lim = dbl_limits;
  250.             i < sizeof(dbl_limits)/sizeof(struct dbl_limits);
  251.             i++, lim++) {
  252.             if ((id.mantissa2 == lim->ieee.mantissa2) &&
  253.                 (id.mantissa1 == lim->ieee.mantissa1) &&
  254.                 (id.exp == lim->ieee.exp)) {
  255.                 vd = lim->d;
  256.                 goto doneit;
  257.             }
  258.         }
  259.         vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS;
  260.         vd.mantissa1 = (id.mantissa1 >> 13);
  261.         vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) |
  262.                 (id.mantissa2 >> 29);
  263.         vd.mantissa3 = (id.mantissa2 >> 13);
  264.         vd.mantissa4 = (id.mantissa2 << 3);
  265.     doneit:
  266.         vd.sign = id.sign;
  267.         *dp = *((double *)&vd);
  268.         return (TRUE);
  269. #endif
  270.  
  271.     case XDR_FREE:
  272.         return (TRUE);
  273.     }
  274.     return (FALSE);
  275. }
  276.